package com.aaa.sbms.dao;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;

public interface QuitDao {

    /**
     * 查询员工
     * @param userid
     * @return
     */
    @Select("select * from users where userid=#{userid}")
    Map user(int userid);

    /**
     * 查询该员工是否已经提交给辞职申请
     * @param userid
     * @return
     */
    @Select("select * from quit where userid=#{userid} and state=0")
    List<Map> isQuit(int userid);

    /**
     * 分页查询离职申请
     * @param map
     * @return
     */
    @Select("<script>" +
            "select q.id,q.userid,q.username,q.sex,q.telephone,\n" +
            "q.idcard,p.pname," +
            "date_format(q.locktime,'%Y-%m-%d %H:%i:%s') locktime\n" +
            "from quit q\n" +
            "left join positions p on p.pid=q.pid\n" +
            "<where>" +
            "and state=0" +
            "<if test='username!=null'>and q.username like '%${username}%'</if>" +
            "</where>"+
            "order by q.id desc limit #{begin},#{pageSize}" +
            "</script>")
    List<Map> quitPage(Map map);

    /**
     * 查询离职申请总数量
     * @param map
     * @return
     */
    @Select("<script>" +
            "select count(*)\n" +
            "from quit q\n" +
            "left join positions p on p.pid=q.pid\n" +
            "<where>" +
            "and state=0" +
            "<if test='username!=null'>and q.username like '%${username}%'</if>" +
            "</where>" +
            "</script>")
    int quitPageCount(Map map);

    /**
     * 分页查询离职员工历史记录
     * @param map
     * @return
     */
    @Select("<script>" +
            "select q.id,q.userid,q.username,q.sex,q.telephone,\n" +
            "q.idcard,q.pname,q.result,q.reason,q.suggest,\n" +
            "date_format(q.locktime,'%Y-%m-%d %H:%i:%s') locktime,\n" +
            "date_format(q.time,'%Y-%m-%d %H:%i:%s') time\n" +
            "from quit_record q\n" +
            "<where>" +
            "<if test='username!=null'>and q.username like '%${username}%'</if>" +
            "</where>"+
            "order by q.id desc limit #{begin},#{pageSize}" +
            "</script>")
    List<Map> quitRecordPage(Map map);

    /**
     * 查询离职员工历史记录总数量
     * @param map
     * @return
     */
    @Select("<script>" +
            "select count(*)\n" +
            "from quit_record q\n" +
            "<where>" +
            "<if test='username!=null'>and q.username like '%${username}%'</if>" +
            "</where>" +
            "</script>")
    int quitRecordPageCount(Map map);

    /**
     * 查询一个辞职申请
     * @param id
     * @return
     */
    @Select("<script>" +
            "select q.id,q.userid,q.username,q.sex,q.telephone,\n" +
            "q.idcard,p.pname,\n" +
            "date_format(q.locktime,'%Y-%m-%d %H:%i:%s') locktime\n" +
            "from quit q\n" +
            "left join positions p on p.pid=q.pid\n" +
            "where id=#{id}" +
            "</script>")
    Map quitOne(int id);

    /**
     * 添加致辞申请
     * @param map
     * @return
     */
    @Insert("insert into quit values(null,#{userid},#{username},#{sex},#{telephone},#{idcard},#{pid}," +
            "date_format(now(),'%Y-%m-%d %H:%i:%s'),0)")
    int addQuit(Map map);

    /**
     * 添加离职历史记录
     * @param map
     * @return
     */
    @Insert("insert into quit_record values(null,#{userid},#{username},#{sex},#{telephone},#{idcard},#{pname},#{locktime}," +
            "#{reason},date_format(now(),'%Y-%m-%d %H:%i:%s'),#{result},#{suggest})")
    int addQuitRecord(Map map);

    /**
     * 修改辞职审核表已审核
     * @return
     */
    @Update("update quit set state=1 where id=#{id}")
    int updateQuitSetState(int id);

    /**
     * 更新uses的一名员工为离职
     * @param userid
     * @return
     */
    @Update("update users set state=1 where userid=#{userid}")
    int updateUsers(int userid);

    /**
     * 删除离职申请
     * @param id
     * @return
     */
    @Delete("delete from quit where id={id}")
    int deleteQuit(int id);
}
